package spring.boot.btc.model;

import lombok.Data;
import spring.boot.btc.utils.HashUtils;

import java.util.List;

/**
 * 一次交易
 * 包含1到多个交易输入和1到多个交易输出
 */
@Data
public class Transaction {

    private String hash;

    private List<TransactionInput> transactionInputList;

    private List<TransactionOutput> transactionOutputList;

    public Transaction() {
    }

    public Transaction(List<TransactionInput> transactionInputList, List<TransactionOutput> transactionOutputList) {
        this.transactionInputList = transactionInputList;
        this.transactionOutputList = transactionOutputList;
        initHash();
    }

    private void initHash() {
        if (transactionInputList.isEmpty() || transactionOutputList.isEmpty()) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        for (TransactionInput input : transactionInputList) {
            sb.append(input.getHash());
        }
        for (TransactionOutput output : transactionOutputList) {
            sb.append(output.getHash());
        }
        hash = HashUtils.sha256(sb.toString());
    }

}
